package com.xframe.common.core.export;

import com.itextpdf.text.DocumentException;
import com.xframe.common.core.constant.ExportFormat;
import com.xframe.common.core.dto.DownFileDto;
import com.xframe.common.core.dto.ErrorCode;
import com.xframe.common.core.exception.BusinessException;
import com.xframe.xdal.core.serialize.JsonConfig;
import com.xframe.xdal.core.serialize.JsonConfigContainer;
import com.xframe.xdal.core.serialize.JsonModel;
import com.xframe.xdal.core.serialize.lang.LanguageEnvironmentContext;
import com.xframe.xdal.core.util.UUIDCreateUtil;
import com.xframe.xdal.core.util.pdf.PdfBuilder;
import com.xframe.xdal.core.util.pdf.TableCol;
import lombok.extern.log4j.Log4j2;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 功能描述:导出文件PDF
 *
 * @author xufeng
 * @version 1.0
 * 2022-09-26
 */
@Log4j2
public class BuilderFileToPdf implements IBuilderFileTo{

    static final String FILE_EXTENSION = ".pdf";


    private void getRefHeadList(String language,String ver,String queryKey,String modelCode,List<TableCol> tableColList,String refAttName){
        JsonConfig refJsonConfig = JsonConfigContainer.getConfigContainer().getJsonConfig(language,ver,
                modelCode,queryKey);
        if(refJsonConfig == null){
            return;
        }
        int index = 1;
        for (JsonModel jsonModel : refJsonConfig.getJsonModelList()) {
            if(jsonModel.isRefModel()){
                if(jsonModel.isRefModel()){
                    // 如果是引用模型
                    getRefHeadList(language,ver,jsonModel.getFlag(),jsonModel.getRefModelCode(),tableColList,refAttName + "." +jsonModel.getMapName());
                }
            }else {
                String headName = LanguageEnvironmentContext.getInstance().getModelAttrText(language, modelCode, jsonModel.getMapName(),jsonModel.getHeader());
                TableCol tableCol = new TableCol(headName, refAttName + "." + jsonModel.getMapName(), index);
                tableColList.add(tableCol);
            }
            index++;
        }
    }

    @Override
    public DownFileDto builderFile(List<JsonModel> headList, Collection values, String filePath, String language, String modelCode
            ,String ver,String queryKey) {
        try {
            DownFileDto dto = new DownFileDto();
            List<TableCol> tableColList = new ArrayList<>();
            int index = 1;
            for (JsonModel jsonModel : headList) {
                if(jsonModel.isRefModel()){
                    if(jsonModel.isRefObj()){
                        // 如果是引用模型
                        getRefHeadList(language,ver,jsonModel.getFlag(),jsonModel.getRefModelCode(),tableColList,jsonModel.getMapName());
                    }
                }else {
                    String headName = LanguageEnvironmentContext.getInstance().getModelAttrText(language, modelCode, jsonModel.getMapName(),jsonModel.getHeader());
                    TableCol tableCol = new TableCol(headName, jsonModel.getMapName(), index);
                    tableColList.add(tableCol);
                }
                index++;
            }
            String uuid = UUIDCreateUtil.getUUID().toString();
            String fileName = uuid + FILE_EXTENSION;
            String fileFullName = filePath + fileName;
            PdfBuilder pdfBuilder = PdfBuilder.create("x", "xufeng", fileFullName);
            pdfBuilder.init().setTable(tableColList, values).saveToPath();
            dto.setFileName(uuid);
            dto.setFileFormat(ExportFormat.PDF.toString().toLowerCase());
            dto.setModelCode(modelCode);
            dto.setLanguage(language);
            return dto;
        } catch (DocumentException e) {
            e.printStackTrace();
            throw new BusinessException(ErrorCode.EXPORT_ERROR,e.getMessage());
        } catch (IOException e) {
            e.printStackTrace();
            throw new BusinessException(ErrorCode.EXPORT_ERROR,e.getMessage());
        }

    }
}
